<?php 
	include('include/database.php');
	include('include/db_functions.php');
	include('include/application_top.php');
?>

<?php
	$check = false;
	$jsCon = 0;
	$message = '';
	$date = date('Y-m-d');
	$brn = $_SESSION['pra_brn'];
	$con = 0;
	$str_chk = mysql_query('select * from tbl_file where SUBSTR(FIL_DATE, 1, 10) = "'.$date.'" and BRN_CODE = "'.$brn.'"');
	if(dboCountRows($str_chk) > 0){
		$message = "You Have Upload Your Daily Cash Already!";
		$check = true;
		$jsCon = 1;
	}
	else{	
		$userid = $_SESSION['pra_userid'];
		if(isset($_POST['ex_reason1'])){
			$file_name =  $_FILES['ex_filename']['name'];
			$ext = pathinfo($file_name, PATHINFO_EXTENSION);
			$t = strtolower($ext);
			if(($t == 'xlsx') || ($t == 'xlsm') || ($t == 'xls') || ($t == 'xlm')){
				$num_user = $_POST['ex_user']; 
				$reason1 = $_POST['ex_reason1'];
				$reason2 = $_POST['ex_reason2'];
				$reason3 = $_POST['ex_reason3'];
				$reason4 = $_POST['ex_reason4'];
				$reason5 = $_POST['ex_reason5'];
				$desc = $_POST['ex_desc'];
				$valid_file = true;
				if($file_name){
					if(!$_FILES['ex_filename']['error']){
						if($_FILES['ex_filename']['size'] > (5242880)){
							$valid_file = false;
							echo '<script> alert("Cannot Upload! Your file is over our system limit!"); </script>';
						}
						if($valid_file){
							if ($ext=='xlsx'){$ext='xls';}
							$new_file_name = strtolower($brn.'_Daily Cash Balance.'.$ext); //rename file  $date.'_'.
							if(move_uploaded_file($_FILES['ex_filename']['tmp_name'], 'document/'.$new_file_name)){
								if(dboInsert(array('FIL_NAME'=>$new_file_name, 'FIL_REASON1'=>$reason1, 'FIL_REASON2'=>$reason2, 'FIL_REASON3'=>$reason3, 'FIL_REASON4'=>$reason4, 'FIL_REASON5'=>$reason5, 'FIL_DESC'=>$desc, 'USE_ID'=>$userid, 'FIL_NUM_USER'=>$num_user, 'BRN_CODE'=>$brn), 'TBL_FILE')){
									$message = "You Have Upload Your Daily Cash Already! ";
									//$message = 'Congratulations!  Your file was accepted.';
									$check = true;
									$con = 1;
									$jsCon = 1;		
								}
							}
							else{
								$con = 3;
							}
						}
					}
					else{
						$message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['ex_filename']['error'];
					}
				}
			}
			else{
				$message = "Oop! Wrong file format, Please Upload only Excel file";
				$con = 2;
	///			echo '<script> alert("Oop! Wrong file format, Please Upload only Excel file"); <script>';
			}
			
		}
	}
?>
<html>
<head>
	<title>Customer Record</title>
    <link type="image/vnd.microsoft.icon" href="img/logo_small.png" rel="shortcut icon">
    <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
		$(document).ready(function(){
			$('#ex_submit').click(function(){
				var filename = $('#ex_filename').val();
				var reason1 = $('#ex_reason1').val();
				var num_user = $('#ex_user').val();
				var desc = $('#ex_desc').val();
				if((reason1 == 0) || (desc == '') || (filename == '') || (num_user == '')){
					alert('Please Fill all the required field!');
				}
				else{
					$("#myform").submit();
				}
			});
			
			var chk = <?php echo $jsCon; ?>;
			if(chk == 1){
				$("#myForm").empty();
				document.getElementById("myForm").innerHTML = "";	 
			}
		})
	</script>
</head>
<body style="margin:0px;">
  <?php require_once('include/top_header.php'); ?>
    <div style="width:100%; height:15px;"></div>
	<div style="width:1000px; height:auto; margin:0 auto; background-color:#FFC">
    <div style="width:100%; height:30px; color:#03F; font-weight:bold;">
    	<?php
			if($check){
				echo $message;
			}
		?>
    </div>
    <form action="index.php" method="post" enctype="multipart/form-data" id="myform" name="myform">
    <table width="500px" style="max-width:500px" align="center" border="0" cellpadding="5px" cellspacing="5px" id="myForm">
       	<tr>
           	<th colspan="2">
           	<p>Daily Cash Upload Form </p>
           	<p>
<?php
$t=time();
echo(date("Y-m-d h:i A",$t));
?> 
</p>
</th>
        </tr>            
		<tr>
			<td colspan="2">
            	<hr /> 
                <b style="width:200px;"><span style="color:#F00;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*</span> is required</b>
             </td>
            
		</tr>
		<tr>
			<td width="30%" align="right">
				Select File<span style="color:#F00;">*</span>: 
			</td>
                    
			<td height="65%">
				<input id="ex_filename" name="ex_filename" type="file" multiple  />
			</td>
		</tr>
        
        <tr>
        	<td align="right">
            	User Input<span style="color:#F00;">*</span>:
            </td>
            
            <td>
            	<select name="ex_user" id="ex_user" style="width:250px;">
                <option value="">--select number of user input--</option>
                <option value="2">2 staffs</option>
                <option value="3">3 staffs</option>
                <option value="4">4 staffs</option>
                <option value="5">5 staffs</option>
                <option value="6">6 staffs</option>
                <option value="7">7 staffs</option>
                <option value="8">8 staffs</option>
                <option value="9">9 staffs</option>
                <option value="10">10 staffs</option>
                </select>
                <!--input type="number" id="ex_user" name="ex_user" style="width:250px;" placeholder="Number of User Input"/-->
            </td>
        </tr> 
		<tr>
			<td align="right">
				Reason1<span style="color:#F00;">*</span>:
			</td>
			<td>
				<select name="ex_reason1" id="ex_reason1" style="width:250px;">
                	<option value="0">--Select your Reason Here--</option>
                        <?php
							$str_query = dboSelectWhereAnd('*', 'tbl_reason', array('REA_STATUS'=>1));
							if(dboCountRows($str_query) > 0){
								foreach(dboResult($str_query) as $data){
									echo '<option value="'.$data["REA_REASON"].'">'.$data["REA_REASON"].'</option>';
								}
							}
						?>
                        </select>
                    </td>
                </tr>
                
                <tr>
                	<td align="right">
                    	Reason2:
                    </td>
                    
                    <td>
                    	<select name="ex_reason2" id="ex_reason2" style="width:250px;">
                        	<option value="0">--Select your Reason Here--</option>
                        	<?php
								$str_query = dboSelectWhereAnd('*', 'tbl_reason', array('REA_STATUS'=>1));
								if(dboCountRows($str_query) > 0){
									foreach(dboResult($str_query) as $data){
										echo '<option value="'.$data["REA_REASON"].'">'.$data["REA_REASON"].'</option>';
									}
								}
							?>
                        </select>
                    </td>
                </tr>
                
                <tr>
                	<td align="right">
                    	Reason3:
                    </td>
                    
                    <td>
                    	<select name="ex_reason3" id="ex_reason3" style="width:250px;">
                        	<option value="0">--Select your Reason Here--</option>
                        	<?php
								$str_query = dboSelectWhereAnd('*', 'tbl_reason', array('REA_STATUS'=>1));
								if(dboCountRows($str_query) > 0){
									foreach(dboResult($str_query) as $data){
										echo '<option value="'.$data["REA_REASON"].'">'.$data["REA_REASON"].'</option>';
									}
								}
							?>
                        </select>
                    </td>
                </tr>
                
                <tr>
                    <td align="right">
                    	Reason4:
                    </td>
                    
                    <td>
                    	<select name="ex_reason4" id="ex_reason4" style="width:250px;">
                        	<option value="0">--Select your Reason Here--</option>
                        	<?php
								$str_query = dboSelectWhereAnd('*', 'tbl_reason', array('REA_STATUS'=>1));
								if(dboCountRows($str_query) > 0){
									foreach(dboResult($str_query) as $data){
										echo '<option value="'.$data["REA_REASON"].'">'.$data["REA_REASON"].'</option>';
									}
								}
							?>
                        </select>
                    </td>
                </tr>
                
                <tr>
                	<td align="right">
                    	Reason5:
                    </td>
                    
                    <td>
                    	<select name="ex_reason5" id="ex_reason5" style="width:250px;">
                        	<option value="0">--Select your Reason Here--</option>
                        	<?php
								$str_query = dboSelectWhereAnd('*', 'tbl_reason', array('REA_STATUS'=>1));
								if(dboCountRows($str_query) > 0){
									foreach(dboResult($str_query) as $data){
										echo '<option value="'.$data["REA_REASON"].'">'.$data["REA_REASON"].'</option>';
									}
								}
							?>
                        </select>
                    </td>
                </tr>
                
                <tr>
                	<td align="right" style="vertical-align:top;">
                    	Description:
                    </td>
                    
                    <td>
                    	<textarea id="ex_desc" name="ex_desc" style="width:250px;"> </textarea>
                    </td>
                </tr>
                
                <tr>
                	<td colspan="2" align="center">
                    	<?php
							if($con == 1){
								echo '<span style="color:#03F; font-weight:bold;">'.$message.'</span>';
							}
							else if($con == 2){
								echo '<span style="color:#FF0000; font-weight:bold;">'.$message.'</span>';
							}
							else if($con == 3){
								echo '<span style="color:#FF0000; font-weight:bold;">File Upload Fail! Please Try Again!</span>';
							}
						?>
                    </td>
                </tr>
                
                <tr>
                	<td colspan="2" align="right">
                    	<input type="button" id="ex_submit" name="ex_submit"  value="Upload"/>
                    </td>
                </tr>  
            </table>
            </form>
    </div>
</div>
<div style="height:70px; width:100%"></div>
<?php include('footer.php'); ?>
</body>
</html>
